package com.XinYun.Library.modules.admin.userControl.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.XinYun.Library.modules.admin.borrowMsg.entity.BorrowMsg;
import com.XinYun.Library.modules.admin.common.JSON.AdminJSON;
import com.XinYun.Library.modules.admin.userControl.service.UserService;
import com.XinYun.Library.modules.admin.userControl.entity.UserInfo;
import com.XinYun.Library.modules.commons.BasicJson;
import com.XinYun.Library.modules.commons.BasicServlet;
import com.XinYun.Library.utils.HTTPRequestUtils.HTTPRequestUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/user/*")
public class UserServlet extends BasicServlet {
    private UserService userService = new UserService();

    /**
     * @param req
     * @param resp
     * @param methodName
     * @throws IOException
     * @throws NoSuchMethodException
     * @throws ServletException
     */
    @SuppressWarnings("AlibabaSwitchStatement")
    private void userDistributor(HttpServletRequest req, HttpServletResponse resp, String methodName) throws IOException, NoSuchMethodException, ServletException {
        //设置返回JSON
        AdminJSON json = new AdminJSON();
        //解析接收JSON
        JSONObject jsonObj = JSONUtil.parseObj(HTTPRequestUtil.getBody(req));
        //方法分配
        switch (methodName){
            case "getMsgRecord"-> userService.getMsg(json,jsonObj);
            case "bannedState"-> userService.updateBanState(json,jsonObj);
            case "getHistory"->userService.getHistory(json,jsonObj);
            default -> throw new NoSuchMethodException("没有该方法");
        }
        //发送
        sendJson(json,resp);
    }

    //获得用户信息分类状态
    public void getMsgRecord(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, NoSuchMethodException {
        userDistributor(req,resp,"getMsgRecord");
    }
    //修改封禁状态
    public void bannedState(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, NoSuchMethodException {
        userDistributor(req,resp,"bannedState");
    }
    //获得用户历史记录
    public void getHistory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, NoSuchMethodException {
        userDistributor(req,resp,"getHistory");
    }
}
